package com.demo.controller.mine;

import cn.hutool.core.util.ObjectUtil;
import com.demo.beans.Pword;
import com.demo.beans.ReturnData;
import com.demo.controller.BaseAction;
import com.demo.service.Impl.PwordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("search")
public class SearchController {
    @Autowired
    private PwordService pwordService;
    @GetMapping("password/getPassword")
    public ReturnData getPassword(@RequestParam("miShi") String miShi,
                                  @RequestParam("userName") String userName,
                                  @RequestParam("condition") String condition){
        ReturnData returnData = BaseAction.createReturnData();
//        String password = "";
        try {
            List<Pword> passwordList = this.pwordService.getPassword(miShi, userName, condition);
            if (ObjectUtil.isNotEmpty(passwordList)){
                returnData.put("returnCode","0000");
                returnData.put("beans",passwordList);
                returnData.put("returnMsg","查询成功");
            }else {
                returnData.put("returnCode","9999");
                returnData.put("returnMsg","查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnData.put("returnCode","9999");
            returnData.put("returnMsg","查询异常：" + e);
            return returnData;
        }
        return returnData;
    }

    @GetMapping("password/getAllPassword")
    public ReturnData getPassword(@RequestParam("miShi") String miShi,
                                  @RequestParam("userName") String userName){
        ReturnData returnData = BaseAction.createReturnData();
//        String password = "";
        try {
            List<Pword> passwordList = this.pwordService.getAllPassword(miShi, userName);
            if (ObjectUtil.isNotEmpty(passwordList)){
                returnData.put("returnCode","0000");
                returnData.put("beans",passwordList);
                returnData.put("returnMsg","查询成功");
            }else {
                returnData.put("returnCode","9999");
                returnData.put("returnMsg","查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnData.put("returnCode","9999");
            returnData.put("returnMsg","查询异常：" + e);
            return returnData;
        }
        return returnData;
    }

    @PostMapping("password/savePassword")
    public ReturnData savePassword(@RequestParam("miShi") String miShi,
                                   @RequestParam("userName") String userName,
                                   @RequestParam("password") String password,
                                   @RequestParam("describe") String describe){
        ReturnData returnData = BaseAction.createReturnData();
        try {
            boolean b = this.pwordService.savePassword(miShi, userName, describe, password);
            if (b){
                returnData.put("returnCode","0000");
                returnData.put("returnMsg","保存成功");
            }else {
                returnData.put("returnCode","9999");
                returnData.put("returnMsg","保存失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnData.put("returnCode","9999");
            returnData.put("returnMsg","保存异常：" + e);
            return returnData;
        }
        return returnData;
    }

    @RequestMapping("password/deletePassword")
    public ReturnData savePassword(@RequestParam("wordId") String wordId){
        ReturnData returnData = BaseAction.createReturnData();
        try {
            boolean b = this.pwordService.deletePassword(wordId);
            if (b){
                returnData.put("returnCode","0000");
                returnData.put("returnMsg","删除成功");
            }else {
                returnData.put("returnCode","9999");
                returnData.put("returnMsg","删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnData.put("returnCode","9999");
            returnData.put("returnMsg","删除异常：" + e);
            return returnData;
        }
        return returnData;
    }
}
